<?php
namespace app\index\controller;
use think\Session;
use think\Controller;
class Wxpay extends Controller
{
    protected $App_Id;
    protected $App_Secret;
    protected $userInfo_url = 'http://printing.jlqy168.com/index/wxpay/getWeixinUser';
    protected function _initialize()
    {
        parent::_initialize();
        $this->App_Id = 'wxb9a7110243353ca0';
        $this->App_Secret = '25ba3b4a91743d6276a1f4a90c1bc3c0';
    }
    /**
     * 注册接口
     */
    public function Oauth(){
        $redirect_uri = urlencode ($this->userInfo_url);
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->App_Id&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
        header("Location:".$url);
    }
    /**
     * 微信注册获取用信息
     */
    public function getWeixinUser(){
        $code = input('code');
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->App_Id&secret=$this->App_Secret&code=$code&grant_type=authorization_code";
        $oauth2 = getJson($oauth2Url);
        $access_token = $oauth2["access_token"];
        $openid = $oauth2['openid'];
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        $userinfo = getJson($get_user_info_url);
        Session::set('user',$userinfo);

        header("Location:".APP_URL);die;
    }
}
?>